/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jigdo.data.template.section;

import jigdo.JigdoException;
import jigdo.data.template.JigdoTemplate;
import jigdo.data.template.TemplateType;
import jigdo.utils.FileUtils;

/**
 * This class represents the basic fields included from all jigdo template files.
 * @author John R Sohn
 */
public abstract class TemplateSection  {
    
    /**
     * Indicates that the system should retain the original datachunk property after parsing
     * for debugging purposes.
     */
    public static boolean RetainDataChunk =false;
   
    public JigdoTemplate Parent;
    
    public TemplateSection(JigdoTemplate parent)
    {
        Parent = parent;
    }
    
    /**
     * Virtual method to be overriden in all descendent classes.
     * Contains logic to parse the raw data chunk into 
     * deserialized data for use by the downloader.
     */
    public void Interpret() throws jigdo.JigdoException
    {
    }    
    
    
    public void ReadSelf(java.io.InputStream file) throws java.io.IOException, JigdoException
    {
        
            try
            {
            Length =  (int)FileUtils.GetIntFromFile(file, 6);
            DataChunk = new byte[Length-10];
                
            file.read(DataChunk,0, Length - 10);
            
            }
            catch (Exception e )
            {
                throw new JigdoException("Error while reading jigdo section.",e);
            }
            
            try
            {
            this.Interpret();
            }
            catch (Exception e)
            {
                throw new JigdoException("Error while interpreting jigdo section",e);
            }
            
    }
    
    public static TemplateSection GetSpecificType(JigdoTemplate parent, String type) throws JigdoException
    {
         switch(type)
                {
                    case "DATA":
                        return new TemplateDATASection(parent);
                      
                    case "DESC":
                        return new TemplateDESCSection(parent);
                        
                    case "BZIP":
                        return new TemplateBZIPSection(parent);
                    default: 
                        throw new JigdoException("Encountered a malformed jigdo template section name:"+type);          
                } 
    }
    
    /**
     * Marks what form of the three supported template types this instance represents.
     * 
     * @see  TemplateType
     */
    public jigdo.data.template.TemplateType SectionType;
    
    /**
     * Represents the length of uncompressed data included in this section.
     */
    public int Length;
    
    /**
     * Contains the raw uninterpreted data read from the .template file as part
     * of this section.
     */
    public byte[] DataChunk;
      
}
